package chaptor1.mapack;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import chaptor1.mapack.Customer;

public class BusinessService {

	public static SessionFactory sessionFactory;


	public void saveCustomer(Customer customer) {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(customer);
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null) {
				tx.rollback();
			}
			throw e;
		} finally {
			session.close();
		}
	}

	public void test() throws Exception{

	    Customer customer=new Customer();
	    customer.setName("Tom");
	    customer.setEmail("lynheell@gmail.com");
	    customer.setPassword("123");
	    saveCustomer(customer);
	  }

	
	public static void main(String args[]) throws Exception {
		try {

			Configuration config = new Configuration().configure();
			//config.addClass(Customer.class);
			sessionFactory = config.buildSessionFactory();

		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		new BusinessService().test();
		sessionFactory.close();
	}

}
